'''
Created on Nov 13, 2010

@author: linh-a
'''
from PyQt4 import QtCore, QtGui, Qt
from core.Category import Category
from util.CategoryManager import CategoryManager
from types import NoneType
class CategoryDialog( QtGui.QDialog ):
  def __init__( self, category = None, conn = None, mode = False, parrent = None ):
    '''
    - True is edit mode
    - False is new mode
    if mode is False, category is parent category
    '''
    super( CategoryDialog, self ).__init__( parrent )
    self.resultCat = None
    self.category = category
    self.conn = conn
    self.mode = mode
    #init elements
    lbName = QtGui.QLabel( "&Name:" )
    lbParent = QtGui.QLabel( "&Parent:" )
    self.cbbParent = QtGui.QComboBox()
    self.txtName = QtGui.QLineEdit()
    lbName.setBuddy( self.txtName )
    lbParent.setBuddy( self.cbbParent )
    buttonBox = QtGui.QDialogButtonBox( QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel )
    grid = QtGui.QGridLayout()
    grid.addWidget( lbName, 0, 0 )
    grid.addWidget( self.txtName, 0, 1, 1, 3 )
    grid.addWidget( lbParent, 1, 0 )
    grid.addWidget( self.cbbParent, 1, 1, 1, 3 )
    grid.addWidget( buttonBox, 2, 0, 1, 4 )
    self.setLayout( grid )
    #title
    if self.mode is False:
      self.setWindowTitle( "New category" )
      self.resultCat = Category()
    else:
      self.setWindowTitle( "Edit %s" % category.get_name() )
      self.resultCat = category
      self.txtName.setText( category.get_name() )
      self.txtName.selectAll()
    self.txtName.setFocus()
    self.connect( buttonBox, Qt.SIGNAL( "accepted()" ), self, Qt.SLOT( "accept()" ) )
    self.connect( buttonBox, Qt.SIGNAL( "rejected()" ), self, Qt.SLOT( "reject()" ) )
  def accept( self ):
    #validate
    isOk = True
    txtMessage = ""
    #check name empty
    strName = self.txtName.text().strip()
    if len( strName ) <= 0:
      isOk = False
      txtMessage = "Category name must not empty"
    #check name exists
    else:
      cur = self.conn.cursor()
      #edit mode
      if self.mode == True:
        params = ( strName, self.category.get_id(), self.category.get_parent() )
        cur.execute( "SELECT * FROM tbCategories WHERE cName LIKE ? and cID != ? and cParent == ?", params )
      else:
        if type( self.category ) is NoneType:
          params = ( strName, 0 )
          
        else:
          params = ( strName, self.category.get_id() )
        cur.execute( "SELECT * FROM tbCategories WHERE cName LIKE ? and cParent == ?", params )
      cats = cur.fetchall()
      if type( cats ) is not NoneType and len( cats ) > 0:
        #exists
        isOk = False
        txtMessage = "This category was already existed. Please enter another."
    if not isOk:
      #alert
      QtGui.QMessageBox.warning( self, "Error", txtMessage, buttons = QtGui.QMessageBox.Ok, defaultButton = QtGui.QMessageBox.Ok )
      #focus
      self.txtName.setFocus()
      return
    #ok --> insert into database
    catMan = CategoryManager( self.conn )
    if self.mode == True:
      self.resultCat.set_name( strName )
      catMan.updateCategory( self.resultCat )
    else:
      #new mode
      #root
      if self.category is None:
        cat = Category( 0, strName, 0 ) 
      #else
      else:
        cat = Category( 0, strName, self.category.get_id() ) 
      catMan.insertCategory( cat )
    QtGui.QDialog.accept( self )
  def reject( self ):
    QtGui.QDialog.reject( self )
